<?php
class ShoppingcartController extends \BaseController {


    //购物车列表显示
    public function getIndex() {
        if(Auth::check()) {
            $userId = Auth::user()->id;
            $products = DB::table('shopping_cart')->where('user_id', $userId)->get();
            $productsArray = array();
            foreach($products as $key => $product) {
                $pictureInfo = Picture::withTrashed()->find($product->picture_id);
                $pictureInfo->exif = json_decode($pictureInfo->exif);
                $products[$key]->pictureInfo = $pictureInfo;

            }
            return View::make('front/shoppingcart')
                    ->with('products', $products);
        }else {
            return "请先登录";
        }

    }

    //获取购物车内容
    public function getProducts() {
        if(Auth::check()) {
            $userId = Auth::user()->id;
            $products = Shoppingcart::where('user_id', '=', $userId)->get();

            return $products;
        }else {
            return "请先登录";
        }
    }

    //添加图片到购物车
    public function postAdd() {
        if(Auth::check()) {
            $pictureId = Input::get('picture_id');
            $userId = Auth::user()->id;

            $isAdded = Shoppingcart::whereRaw('user_id = ? and picture_id = ?', array($userId, $pictureId))->get();

            if(count($isAdded) != 0) {
                return array(
                    'status' => false,
                    'message' => '已在购物车',
                );
            }

            $product = new Shoppingcart();
            $product->user_id = $userId;
            $product->picture_id = $pictureId;
            $isSuccess = $product->save();

            if($isSuccess) {
                $cartNum = Shoppingcart::where('user_id', '=', $userId)->count();
                Session::put('cartNum', $cartNum);
                $result = array(
                    'status' => true,
                    'message' => '添加成功',
                    'count' => $cartNum
                );
                return $result;
            }else {
                $result = array(
                    'status' => false,
                    'message' => '添加失败',
                );
                return $result;
            }
        }else {
            return "请先登录";
        }
    }

    //从购物车删除图片
    public function getDelete($pictureId) {
        if(Auth::check()) {
            $userId = Auth::user()->id;

            $product = Shoppingcart::whereRaw('user_id = ? and picture_id = ?', array($userId, $pictureId))->delete();
            if($product) {
                $cartNum = Shoppingcart::where('user_id', '=', $userId)->count();
                Session::put('cartNum', $cartNum);
                return Redirect::to('/shoppingcart')
                    ->with('message', '成功删除一条记录！');
            }else {
                return Redirect::to('/shoppingcart')
                    ->with('error', '删除失败！');
            }
        }else {
            return "请先登录";
        }
    }

}
